package com.freak.wanandroid.model;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.freak.wanandroid.R;
import com.freak.wanandroid.app.App;
import com.freak.wanandroid.base.activity.BaseActivity;
import com.freak.wanandroid.base.adapter.MainTabAdapter;
import com.freak.wanandroid.model.classify.ClassifyFragment;
import com.freak.wanandroid.model.discover.DiscoverFragment;
import com.freak.wanandroid.model.home.HomepageFragment;
import com.freak.wanandroid.model.myself.MyselfFragment;
import com.freak.wanandroid.model.navigation.NavigationFragment;
import com.freak.wanandroid.model.search.SearchActivity;
import com.freak.wanandroid.util.ToastUtil;
import com.freak.wanandroid.view.viewpager.DepthPageTransformer;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private static final List<String> PAGE_TITLE_LIST = new ArrayList<>();

    private static final List<Fragment> PAGE_FRAGMENT_LIST = new ArrayList<>();

    private static final int PAGE_HOME = 0;

    private static final int PAGE_SYSTEM = 1;

    private static final int PAGE_NAVIGATION = 2;

    private static final int PAGE_CLASSIFY = 3;

    private static final int PAGE_MYSELF = 4;

    private ViewPager2 mViewPageMain;

    private MainTabAdapter mMainTabAdapter;

    private final long BACK_PRESS_TIME = 2000;

    private LinearLayout mLinearLayoutHomepage, mLinearLayoutSystem, mLinearLayoutNavigation, mLinearLayoutClassify, mLinearLayoutMyself;

    private long lastTimeMillis;

    private LinearLayout mSearch;
    private LinearLayout mLinearLayoutMessage;
    private LinearLayout mLinearLayoutFunction;

    private TextView mTitle;

    static {
        PAGE_TITLE_LIST.add("首页");
//        PAGE_TITLE_LIST.add("体系");
        PAGE_TITLE_LIST.add("发现");
        PAGE_TITLE_LIST.add("导航");
        PAGE_TITLE_LIST.add("项目分类");
        PAGE_TITLE_LIST.add("我的");
        PAGE_FRAGMENT_LIST.add(new HomepageFragment());
//        PAGE_FRAGMENT_LIST.add(new SystemFragment());
        PAGE_FRAGMENT_LIST.add(new DiscoverFragment());
        PAGE_FRAGMENT_LIST.add(new NavigationFragment());
        PAGE_FRAGMENT_LIST.add(new ClassifyFragment());
        PAGE_FRAGMENT_LIST.add(new MyselfFragment());
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }


    @Override
    protected void initEventAndData() {
        initOnClick();
    }

    @Override
    protected void onDestroyRelease() {

    }

    public static void startAction(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    private void initOnClick() {
        mLinearLayoutHomepage.setOnClickListener(this);
        mLinearLayoutSystem.setOnClickListener(this);
        mLinearLayoutNavigation.setOnClickListener(this);
        mLinearLayoutClassify.setOnClickListener(this);
        mLinearLayoutMyself.setOnClickListener(this);
        mLinearLayoutMessage.setOnClickListener(this);
        mSearch.setOnClickListener(this);
        mLinearLayoutFunction.setOnClickListener(this);
    }

    @Override
    protected void initView() {
        mLinearLayoutHomepage = getViewById(R.id.ll_homepage);
        mLinearLayoutSystem = getViewById(R.id.ll_system);
        mLinearLayoutNavigation = getViewById(R.id.ll_navigation);
        mLinearLayoutClassify = getViewById(R.id.ll_classify);
        mLinearLayoutMyself = getViewById(R.id.ll_myself);
        mSearch = getViewById(R.id.search);
        mTitle = getViewById(R.id.title);
        mLinearLayoutMessage = getViewById(R.id.linearLayoutMessage);
        mLinearLayoutFunction = getViewById(R.id.linearLayoutFunction);
        mViewPageMain = getViewById(R.id.viewPageMain);
        mMainTabAdapter = new MainTabAdapter(this, PAGE_FRAGMENT_LIST, PAGE_TITLE_LIST);
        mViewPageMain.setAdapter(mMainTabAdapter);
        mViewPageMain.setOffscreenPageLimit(PAGE_FRAGMENT_LIST.size());
        mViewPageMain.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                setSelect(position);
            }
        });
        mViewPageMain.setPageTransformer(new DepthPageTransformer());
        setSelect(0);
    }

    public void setSelect(int position) {
        mViewPageMain.setCurrentItem(position);
        mTitle.setText(PAGE_TITLE_LIST.get(position));
        setSelectTab(position);
    }

    private void setSelectTab(int position) {
        mLinearLayoutMessage.setVisibility(View.GONE);
        mSearch.setVisibility(View.VISIBLE);
        mLinearLayoutFunction.setVisibility(View.VISIBLE);
        switch (position) {
            case PAGE_HOME:
                mLinearLayoutHomepage.setSelected(true);
                mLinearLayoutSystem.setSelected(false);
                mLinearLayoutNavigation.setSelected(false);
                mLinearLayoutClassify.setSelected(false);
                mLinearLayoutMyself.setSelected(false);
                break;
            case PAGE_SYSTEM:
                mLinearLayoutHomepage.setSelected(false);
                mLinearLayoutSystem.setSelected(true);
                mLinearLayoutNavigation.setSelected(false);
                mLinearLayoutClassify.setSelected(false);
                mLinearLayoutMyself.setSelected(false);
                break;
            case PAGE_NAVIGATION:
                mLinearLayoutHomepage.setSelected(false);
                mLinearLayoutSystem.setSelected(false);
                mLinearLayoutNavigation.setSelected(true);
                mLinearLayoutClassify.setSelected(false);
                mLinearLayoutMyself.setSelected(false);
                break;
            case PAGE_CLASSIFY:
                mLinearLayoutHomepage.setSelected(false);
                mLinearLayoutSystem.setSelected(false);
                mLinearLayoutNavigation.setSelected(false);
                mLinearLayoutClassify.setSelected(true);
                mLinearLayoutMyself.setSelected(false);
                break;
            case PAGE_MYSELF:
                mLinearLayoutHomepage.setSelected(false);
                mLinearLayoutSystem.setSelected(false);
                mLinearLayoutNavigation.setSelected(false);
                mLinearLayoutClassify.setSelected(false);
                mLinearLayoutMyself.setSelected(true);
                mLinearLayoutMessage.setVisibility(View.VISIBLE);
                mSearch.setVisibility(View.GONE);
                mLinearLayoutFunction.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        myselfFragment.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_homepage:
                setSelect(PAGE_HOME);
                break;
            case R.id.ll_system:
                setSelect(PAGE_SYSTEM);
                break;
            case R.id.ll_navigation:
                setSelect(PAGE_NAVIGATION);
                break;
            case R.id.ll_classify:
                setSelect(PAGE_CLASSIFY);
                break;
            case R.id.ll_myself:
                setSelect(PAGE_MYSELF);
                break;
            case R.id.search:
                SearchActivity.startAction(this);
                break;
            case R.id.linearLayoutMessage:
                break;
//            case R.id.often:
//                FrequentlyActivity.startAction(this);
//                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - lastTimeMillis <= BACK_PRESS_TIME) {
            App.getInstance().removeAllActivity();
            super.onBackPressed();
        } else {
            lastTimeMillis = System.currentTimeMillis();
            ToastUtil.showShort(this, getString(R.string.backpress_again_finish));
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        int tab = getIntent().getIntExtra("tab", 0);
        setSelect(tab);
    }
}
